package com.example.administrator.ttphonehelper.app;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.administrator.ttphonehelper.R;
import com.example.administrator.ttphonehelper.bean.AppInfoBean;

import java.util.ArrayList;
import java.util.List;

public class AppActivity extends AppCompatActivity implements View.OnClickListener,
        AdapterView.OnItemClickListener,CompoundButton.OnCheckedChangeListener{
    private GridView mGridView;
    private List<AppInfoBean> mList1;
    private List<AppInfoBean> mList2;
    private MyAppAdapter mAdapter;

    private ProgressBar mPbLoading;
    private PackageManager mPackageManager;
    private ImageView mIvShowApp;
    private TextView mTVAppCount;
    private RadioButton mRbUser;
    private RadioButton mRbSys;
    //判断Grid的显示方式
    private static final int NUM_COLUMS1 = 1;
    private static final int NUM_COLUMS2 = 2;
    private int mState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        //初始化控件
        mGridView = (GridView) findViewById(R.id.gv_app);
        mPbLoading = (ProgressBar) findViewById(R.id.pb_app_loading);
        mIvShowApp = (ImageView) findViewById(R.id.iv_show_app);
        mTVAppCount = (TextView) findViewById(R.id.tv_app_count);
        mRbUser = (RadioButton) findViewById(R.id.rb_user_app);
        mRbSys = (RadioButton) findViewById(R.id.rb_sys_app);

        mPackageManager = getPackageManager();

        mState = NUM_COLUMS1;
        //设置适配器
        mAdapter = new MyAppAdapter(this,mState);
        mGridView.setAdapter(mAdapter);
        //加载数据
        loadData();
        //添加点击事件
        mIvShowApp.setOnClickListener(this);
    }
    //在子线程中加载数据
    private void loadData() {
        mPbLoading.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                mList1 = new ArrayList<>();
                mList2 = new ArrayList<>();
                List<PackageInfo> infos = mPackageManager.getInstalledPackages(0);
                for (PackageInfo app:infos)
                {
                    //判断是否是系统应用 app.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM !=0
                    if((app.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) !=0){
                        String lable = app.applicationInfo.loadLabel(mPackageManager).toString();
                        Log.i("info",lable);
                        String pkgName = app.applicationInfo.packageName;
                        Drawable icon = app.applicationInfo.loadIcon(mPackageManager);
                        //第一种显示模式数据
                        mList1.add(new AppInfoBean(lable,icon));
                        //第二种显示模式数据
                        mList2.add(new AppInfoBean(lable,pkgName,icon));
                    }
                    else{
                        String lable = app.applicationInfo.loadLabel(mPackageManager).toString();
                        Log.i("info",lable);
                        String pkgName = app.applicationInfo.packageName;
                        Drawable icon = app.applicationInfo.loadIcon(mPackageManager);
                        mList1.add(new AppInfoBean(lable,icon));
                        mList2.add(new AppInfoBean(lable,pkgName,icon));
                    }

                }
                //主线程中运行以下代码
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //放入数据
                        mAdapter.setData(mList1);
                        //刷新界面
                        mAdapter.notifyDataSetChanged();
                        //关闭加载框
                        mPbLoading.setVisibility(View.GONE);
                        //APP数量
                        mTVAppCount.setText("用户应用数："+ mList1.size());
                    }
                });
            }
        }).start();
    }


    @Override
    public void onClick(View v) {
        if(mState==NUM_COLUMS1){
            mState = NUM_COLUMS2;
            mAdapter.setTag(mState);
            mAdapter.setData(mList2);
            mGridView.setNumColumns(1);
            mAdapter.notifyDataSetChanged();
            mIvShowApp.setImageResource(R.mipmap.list);
        }
        else {
            mState = NUM_COLUMS1;
            mAdapter.setTag(mState);
            mAdapter.setData(mList1);
            mGridView.setNumColumns(3);
            mAdapter.notifyDataSetChanged();
            mIvShowApp.setImageResource(R.mipmap.grids);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
